package com.wetao.lib_common.common.adapter;

import android.app.Service;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wetao.lib_common.R;
import com.wetao.lib_common.utils.WifiSupport;

import java.util.List;

/**
 * Date :2018/8/2
 * Time :8:59
 * author:moyihen
 */

public class WifiRelayListAdapter extends RecyclerView.Adapter<WifiRelayListAdapter.ViewHolder> {



    private  List<ScanResult> mList;
    Context mContext;
    private OnItemClickListener mOnItemClickListener;

    private String TAG="WifiAdapter";


    private int selectedIndex;        //记录当前选中的条目索引

    public void setSelectedIndex(int position) {
        this.selectedIndex = position;
        notifyDataSetChanged();
    }





    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }


    public WifiRelayListAdapter(Context context, List<ScanResult> list) {
        this.mContext=context;
        this.mList = list;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wifi_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ScanResult resut=mList.get(position);

        holder.mName.setText(resut.SSID.trim());


        if (selectedIndex == position) {

            holder.mName.setSelected(true);
        } else {                                                            //非选中状态

            holder.mName.setSelected(false);
        }


        boolean lock_type = true;
        String lock_str;
        if (resut.capabilities.contains("WPA2-PSK")) {
            // WPA-PSK加密
            lock_str = "通过WPA2-PSK进行保护";
        } else if (resut.capabilities.contains("WPA-PSK")) {
            // WPA-PSK加密
            lock_str = "通过WPA-PSK进行保护";
        } else if (resut.capabilities.contains("WPA-EAP")) {
            // WPA-EAP加密
            lock_str = "通过WPA-EAP进行保护";
        } else if (resut.capabilities.contains("WEP")) {
            // WEP加密
            lock_str = "通过WEP进行保护";
        } else {
            // 无密码
            lock_str = "开放网络";
            lock_type = false;
        }



        int level = resut.level;

        setLevel(level,lock_type,holder);
        WifiManager wifiManager = (WifiManager) mContext
                .getSystemService(Service.WIFI_SERVICE);

        if (WifiSupport.isExsits(resut.SSID,mContext) != null
                && WifiSupport.isExsits(resut.SSID,mContext).networkId == wifiManager
                .getConnectionInfo().getNetworkId()){
            holder.link.setImageResource(R.drawable.common_wifi_link);

            System.out.println(wifiManager
                    .getConnectionInfo().getNetworkId());
            System.out.println(resut.SSID);
            holder.link.setVisibility(View.VISIBLE);
        }else{
            holder.link.setVisibility(View.INVISIBLE);
        }


        int adapterPosition = holder.getAdapterPosition();
        if (mOnItemClickListener !=null){
            holder.itemView.setOnClickListener(new MyOnClickListener(position,mList.get(adapterPosition)));
        }


    }
    //设置值网络强度
    private void setLevel(int level, boolean desc, ViewHolder holder) {
        //是否加密
        if(desc){
            holder.wifipassword.setVisibility(View.VISIBLE);
            holder.wifipassword.setImageResource(R.drawable.common_wifi_pass);
        }else{
            holder.wifipassword.setVisibility(View.INVISIBLE);
        }


                // 网络信号强度
                int imgId;
                if (Math.abs(level) > 85) {
                    imgId = R.drawable.common_wifi_black_1;
                } else if (Math.abs(level) > 70) {
                    imgId = R.drawable.common_wifi_black_2;
                } else if (Math.abs(level) > 60) {
                    imgId = R.drawable.common_wifi_black_3;
                } else if (Math.abs(level) > 50) {
                    imgId = R.drawable.common_wifi_black_4;
                } else {
                    imgId = R.drawable.common_wifi_black_4;
                }
                holder.mLevel.setImageResource(imgId);



    }

    @Override
    public int getItemCount() {
        return mList.size();
    }



    class ViewHolder extends RecyclerView.ViewHolder{
        private  TextView mName;
        private  ImageView wifipassword;
        private  ImageView mLevel;
        private  ImageView link;
        ViewHolder(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.wifiname);
            mLevel = itemView.findViewById(R.id.wifi_icon);
            wifipassword = itemView.findViewById(R.id.wifipassword);
            link = itemView.findViewById(R.id.link);
        }
    }


    class MyOnClickListener implements View.OnClickListener {
        private  int position;
        private  ScanResult data;

        MyOnClickListener(int position, ScanResult data){
            this.position = position;
            this.data = data;
        }
        @Override
        public void onClick(View view) {
            mOnItemClickListener.onItemClick(view,position,data);
        }
    }


    public interface OnItemClickListener{
        void onItemClick(View view, int position, ScanResult data);
    }

}
